from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from typing import List

from ..core.database import get_db
from ..utils.deps import get_current_user
from ..models.user import User
from ..schemas.tag import TagCreate, TagUpdate, TagResponse
from ..services.tag_service import TagService

router = APIRouter(prefix="/tags", tags=["标签"])


@router.get("", response_model=List[TagResponse])
async def get_tags(
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """获取所有标签"""
    tags = TagService.get_tags(current_user.id, db)
    return tags


@router.get("/{tag_id}", response_model=TagResponse)
async def get_tag(
    tag_id: int,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """获取标签详情"""
    tag = TagService.get_tag(tag_id, current_user.id, db)
    return tag


@router.post("", response_model=TagResponse, status_code=201)
async def create_tag(
    tag_data: TagCreate,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """创建标签"""
    tag = TagService.create_tag(tag_data, current_user.id, db)
    return tag


@router.put("/{tag_id}", response_model=TagResponse)
async def update_tag(
    tag_id: int,
    tag_data: TagUpdate,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """更新标签"""
    tag = TagService.update_tag(tag_id, tag_data, current_user.id, db)
    return tag


@router.delete("/{tag_id}", status_code=204)
async def delete_tag(
    tag_id: int,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """删除标签"""
    TagService.delete_tag(tag_id, current_user.id, db)
    return None

